<?php
namespace App\Services\ChuangLan;


class ChuanglanSmsApi implements ChuangLanSmsContract{
	private $api_account = 'vip-37hpz';//账号
	private $api_password = 'Tch783530  ';//密码
	private $api_send_url = 'http://222.73.117.156/msg/HttpBatchSendSM';//接口地址
	private $api_balance_query_url = 'http://222.73.117.156/msg/QueryBalance';//余额查询地址
	/**
	 * 发送短信
	 *
	 * @param string $mobile 手机号码
	 * @param string $msg 短信内容
	 * @param string $needstatus 是否需要状态报告
	 * @param string $product 产品id，可选
	 * @param string $extno   扩展码，可选
	 */
	public function sendSMS( $mobile, $msg, $needstatus = 'false', $extno = '') {
		//创蓝接口参数
		$postArr = array (
			'account' => $this->api_account,
			'pswd' => $this->api_password,
			'msg' => $msg,
			'mobile' => $mobile,
			'needstatus' => $needstatus,
			'extno' => $extno
		);

		$result = $this->curlPost( $this->api_send_url , $postArr);
//        weiXinNotifyLog('mobile:'.$mobile.',sms_return_code:'.$result);
		return $this->getRes($result);
	}

	/**
	 * 查询额度
	 *
	 *  查询地址
	 */
	public function queryBalance() {
		global $chuanglan_config;
		//查询参数
		$postArr = array (
			'account' => $this->api_account,
			'pswd' => $this->api_password,
		);
		$result = $this->curlPost($this->api_balance_query_url, $postArr);
		return $result;
	}

	/**
	 * 处理返回值
	 *
	 */
	public function execResult($result){
		$result=preg_split("/[,\r\n]/",$result);
		return $result;
	}

	/**
	 * 通过CURL发送HTTP请求
	 * @param string $url  //请求URL
	 * @param array $postFields //请求参数
	 * @return mixed
	 */
	private function curlPost($url,$postFields){
		$postFields = http_build_query($postFields);
		$ch = curl_init ();
		curl_setopt ( $ch, CURLOPT_POST, 1 );
		curl_setopt ( $ch, CURLOPT_HEADER, 0 );
		curl_setopt ( $ch, CURLOPT_RETURNTRANSFER, 1 );
		curl_setopt ( $ch, CURLOPT_URL, $url );
		curl_setopt ( $ch, CURLOPT_POSTFIELDS, $postFields );
		$result = curl_exec ( $ch );
		curl_close ( $ch );
		return $result;
	}

	//魔术获取
	public function __get($name){
		return $this->$name;
	}

	//魔术设置
	public function __set($name,$value){
		$this->$name=$value;
	}

	//短信返回码
	public function getRes($res){
		$res_arr = explode(',',$res);
		$data = [
			'code' => -1,
			'message' => '未知错误'
		];
		switch(last($res_arr)){
			case 0:
				$data = [
					'code' => last($res_arr),
					'message' => '提交成功'
				];
				break;
			case 101:
				$data = [
					'code' => last($res_arr),
					'message' => '无此用户'
				];
				break;
			case 102:
				$data = [
					'code' => last($res_arr),
					'message' => '密码错误'
				];
				break;
			case 103:
				$data = [
					'code' => last($res_arr),
					'message' => '提交过快（提交速度超过流速限制）'
				];
				break;
			case 104:
				$data = [
					'code' => last($res_arr),
					'message' => '系统忙（因平台侧原因，暂时无法处理提交的短信）'
				];
				break;
			case 105:
				$data = [
					'code' => last($res_arr),
					'message' => '敏感短信（短信内容包含敏感词）'
				];
				break;
			case 106:
				$data = [
					'code' => last($res_arr),
					'message' => '消息长度错（>536或<=0）'
				];
				break;
			case 107:
				$data = [
					'code' => last($res_arr),
					'message' => '包含错误的手机号码'
				];
				break;
			case 108:
				$data = [
					'code' => last($res_arr),
					'message' => '手机号码个数错（群发>50000或<=0;单发>200或<=0）'
				];
				break;
			case 109:
				$data = [
					'code' => last($res_arr),
					'message' => '无发送额度（该用户可用短信数已使用完）'
				];
				break;
			case 110:
				$data = [
					'code' => last($res_arr),
					'message' => '不在发送时间内'
				];
				break;
			case 111:
				$data = [
					'code' => last($res_arr),
					'message' => '超出该账户当月发送额度限制'
				];
				break;
			case 112:
				$data = [
					'code' => last($res_arr),
					'message' => '无此产品，用户没有订购该产品'
				];
				break;
			case 113:
				$data = [
					'code' => last($res_arr),
					'message' => 'extno格式错（非数字或者长度不对）'
				];
				break;
			case 115:
				$data = [
					'code' => last($res_arr),
					'message' => '自动审核驳回'
				];
				break;
			case 116:
				$data = [
					'code' => last($res_arr),
					'message' => '签名不合法，未带签名（用户必须带签名的前提下）'
				];
				break;
			case 117:
				$data = [
					'code' => last($res_arr),
					'message' => 'IP地址认证错,请求调用的IP地址不是系统登记的IP地址'
				];
				break;
			case 118:
				$data = [
					'code' => last($res_arr),
					'message' => '用户没有相应的发送权限'
				];
				break;
			case 119:
				$data = [
					'code' => last($res_arr),
					'message' => '用户已过期'
				];
				break;
			case 120:
				$data = [
					'code' => last($res_arr),
					'message' => '测试内容不是白名单'
				];
				break;
		}

		return $data;
	}
}
?>